<template>
  <!-- 修改配送方式 -->
  <el-dialog title="修改配送方式" v-model="editVisible" width="30%" draggable>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="150px" v-loading="loading">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="配送方式：" prop="delivery_type">
            <el-select v-model.trim="form.delivery_type" placeholder="请选择配送方式" clearable class="">
              <el-option v-for="item in deliveryTypeList" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="disabledbtn" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useState } from "@/hooks/useState.ts";
import type { IDictData } from "@/types/dict";
import type { IDeliveryTypeDTO } from "@/types/rentMachineManage/rentMachineOrderInfo";
import { putChangeDeliveryTypeInfo } from "@/utils/api/admin/order/rentOrder";
const { disabledbtn, loading, toggleState } = useState();
interface Props {
  deliveryTypeList: Array<IDictData>;
}
const { deliveryTypeList } = defineProps<Props>();

const emit = defineEmits<{
  handleSuccess: [];
}>();
const editVisible = defineModel<boolean>("editVisible", { required: true });
const form = defineModel<IDeliveryTypeDTO>("form", { required: true });
const formRef = useTemplateRef("formRef");
const rules = reactive({
  delivery_type: [{ required: true, message: "请选择配送方式", trigger: "blur" }]
});

watch(
  () => editVisible.value,
  newValue => {
    if (newValue) {
      /* 初始化数据 */
    } else {
      form.value.id = 0;
      form.value.delivery_type = "";
      formRef.value?.resetFields();
      formRef.value?.clearValidate();
    }
  }
);
/* 数据校验 */
function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning("请填写必填字段");
      return;
    }
    submitFormInfo();
  });
}
/* 保存或编辑信息 */
function submitFormInfo() {
  toggleState(true);
  putChangeDeliveryTypeInfo(form.value)
    .then(res => {
      ElMessage.success(res.msg);
      emit("handleSuccess");
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}
</script>

<style lang="scss" scoped></style>
